package com.stormrage.edu.ctrl.role;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.adm.AdmAcctServ;
import com.stormrage.edu.serv.adm.AdmResServ;
import com.stormrage.entity.adm.AdmAcct;
import com.stormrage.entity.adm.AdmRes;
import com.stormrage.entity.adm.rep.AdmAcctRep;
import com.stormrage.entity.adm.rep.AdmPermsRep;
import com.stormrage.entity.adm.rep.AdmResRep;
import com.stormrage.entity.adm.req.AdmAcctReq;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.shiro.ShiroAcct;
import com.stormrage.shiro.bind.CurrentUser;
import com.stormragetech.core.ctrl.BaseCtrl;

@Controller
@RequestMapping("/role")
public class AdmRoleCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(AdmRoleCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Autowired
	private AdmAcctServ admAcctServ;

	@Autowired
	private AdmResServ admResServ;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 index 】-【 GET 】- 开始运行 ");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryRoleMenuById(Integer.valueOf(curr.getId()));

			logger.debug(" 菜单为 " + menus.toString());

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 index 】-【 GET 】- 结束运行 ");
		return "role/index";

	}

	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 create 】-【 GET 】- 开始运行 ");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryRoleMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			List<AdmResRep> array = admResServ.queryList(null);

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 create 】-【 GET 】- 结束运行 ");
		return "role/create";

	}

	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@CurrentUser ShiroAcct curr, AdmAcctReq req, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 create 】-【 POST 】- 开始运行 ");

		ErrorRep err = null;
		String errMsg = "";

		logger.debug(" edit请求为 " + req.toString());

		try {

			// ------------------ Ctrl操作 ------------------
			Integer index = admAcctServ.create(req);
			errMsg = "提示信息：创建ID为【" + index + "】的管理员成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：创建管理员失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/role/create";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 create 】-【 POST 】- 结束运行 ");
		return "redirect:/role/list";

	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/pass/{id}", method = RequestMethod.GET)
	public String pass(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 pass 】-【 GET 】- 开始运行 ");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ 获取管理员资料 ------------------
			AdmAcct entity = admAcctServ.queryById(id);
			AdmAcctReq req = AdmAcctReq.instance(entity.getAcct(), "123456", null, null, null);

			// ------------------ Ctrl操作 ------------------
			admAcctServ.updatePass(req);
			errMsg = "提示信息：修改ID为【" + id + "】的管理员的密码成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：修改ID为【" + id + "】的管理员的密码失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/role/list";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 pass 】-【 POST 】- 结束运行 ");
		return "redirect:/role/list";

	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/lock/{id}", method = RequestMethod.GET)
	public String lock(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 pass 】-【 POST 】- 开始运行 ");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ Ctrl操作 ------------------
			admAcctServ.updateLocked(id);
			errMsg = "提示信息：修改ID为【" + id + "】的管理员的锁定状态成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：修改ID为【" + id + "】的管理员的锁定状态失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/role/list";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 pass 】-【 POST 】- 结束运行 ");
		return "redirect:/role/list";

	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/perms/{id}", method = RequestMethod.GET)
	public String perms(@CurrentUser ShiroAcct curr, @PathVariable("id") Integer id, Model model) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 perms 】-【 GET 】- 开始运行 ");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryRoleMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			AdmPermsRep rep = admAcctServ.queryPermsRep(id);

			logger.debug(" 用户现有权限为 " + rep.getList().toString());

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", rep);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 perms 】-【 GET 】- 结束运行 ");
		return "role/perms";

	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/perms/{id}", method = RequestMethod.POST)
	public String perms(@CurrentUser ShiroAcct curr, AdmAcctReq req, RedirectAttributes redirect) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 perms 】-【 POST 】- 开始运行 ");

		ErrorRep err = null;
		String errMsg = "";

		logger.debug(" edit请求为 " + req.toString());

		try {

			// ------------------ Ctrl操作 ------------------
			admAcctServ.updateRole(req);
			errMsg = "提示信息：创建ID为【" + req.getId() + "】的管理员的权限成功";
			err = ErrorRep.instance(true, errMsg);
			redirect.addFlashAttribute("err", err);

		} catch (Exception e) {
			errMsg = "提示信息：创建ID为【" + req.getId() + "】的管理员的权限失败";
			err = ErrorRep.instance(false, errMsg);
			redirect.addFlashAttribute("err", err);
			return "redirect:/role/perms/{id}";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 perms 】-【 POST 】- 结束运行 ");
		return "redirect:/role/list";

	}

	@RequiresPermissions("role:view")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@CurrentUser ShiroAcct curr, Model model) {

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 list 】-【 GET 】- 开始运行 ");

		try {

			// ------------------ 获取用户操作的 Menu ------------------
			List<AdmRes> menus = admAcctServ.queryRoleMenuById(Integer.valueOf(curr.getId()));

			// ------------------ Ctrl操作 ------------------
			List<AdmAcctRep> array = admAcctServ.queryList();

			model.addAttribute("user", curr);
			model.addAttribute("menus", menus);
			model.addAttribute("htmlTitle", htmlTitle);

			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug(" 【 Controller 】-【 AdmRoleCtrl 】-【 list 】-【 GET 】- 结束运行 ");
		return "role/list";

	}

}
